//
//  AppDelegate.m
//  音伴尔家听宝
//
//  Created by Burgess on 2017/4/7.
//  Copyright © 2017年 Burgess. All rights reserved.
//

#import "AppDelegate.h"
#import "LYLoginViewController.h"
#import"YbrDLNAUtil.h"
#import <AVFoundation/AVFoundation.h>
#import "HTTPServer.h"
#import "YbrSystemUtil.h"
#import "YbrSQLHelper.h"
#import "YbrChannelManage.h"
#import "YbrRecordBgMusicManage.h"
#import "YbrLocalMusicDao.h"

@interface AppDelegate ()

@end

@implementation AppDelegate{
    AVAudioPlayer   * audioPlayer;
    HTTPServer      * httpServer;
}



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    NSURLCache *URLCache = [[NSURLCache alloc] initWithMemoryCapacity:4 * 1024 * 1024
                                                         diskCapacity:20 * 1024 * 1024
                                                             diskPath:nil];
    //内存4M，硬盘20M
    [NSURLCache setSharedURLCache:URLCache];

    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    
    //这样设置不了状态栏的颜色
    //    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
    LYLoginViewController *loginVC = [[LYLoginViewController alloc]init];
    self.window.rootViewController = loginVC;
    [self.window makeKeyAndVisible];
    
    
    // 第一次的跳转问题
    NSUserDefaults * defaults       =   [NSUserDefaults standardUserDefaults];
    [defaults setBool:NO forKey:@"isUploading"];
    [defaults setBool:YES forKey:@"secMain"];

    // 第一次使用APP,初始化dB,并跳转到引导页面
    if(![defaults boolForKey:@"isFirst"]){
        
        [defaults setBool:YES forKey:@"isFirst"];
        CFUUIDRef cfuuid            =   CFUUIDCreate(kCFAllocatorDefault);
        NSString *cfuuidString      =   (NSString*)CFBridgingRelease(CFUUIDCreateString(kCFAllocatorDefault, cfuuid));
        [defaults setObject:cfuuidString forKey:@"DeviceId"];
        
        [YbrSQLHelper getDataBase];
        [[YbrSQLHelper alloc] createDb];
        [[YbrChannelManage alloc] InitDB];
        [[YbrRecordBgMusicManage alloc] InitDB];
        
        // 第一次初始化
        [[[YbrLocalMusicDao alloc] init] firstSetLocalMusic];

}
    return YES;
}
// 准备启动手机的局域网服务器,目录为Document
//-(void) setBackGroud {
//    httpServer      =   [[HTTPServer alloc] init];
//    [httpServer setType:@"_http._tcp."];
//    [httpServer setDocumentRoot:getDocument];
//    [self startServer];
//}

// 开启局域网服务器
//- (void)startServer
//{
//    NSError * error;
//    NSString * currentIp    =   [YbrSystemUtil getDeviceCurrentIpAddress];
//    
//    NSLog(@"------------------------ 测试服务器有没有重启---------------------    %@   ---- ",currentIp);
//    
//    if([httpServer start:&error]){
//        NSLog(@"Started HTTP Server on port %@:%hu  %@", currentIp ,[httpServer listeningPort] ,getDocument);
//        [self.perfer setHttpServiceUri:[NSString stringWithFormat:@"http://%@:%i/" ,currentIp ,[httpServer listeningPort]]];
//        [self.perfer edited];
//    }
//    else{
//        NSLog(@"Error starting HTTP Server: %@", error);
//    }
//}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
        [[YbrDLNAUtil getInstance] setFastScan];   // 重新开始扫描设备
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


@end
